package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnFarmSpecificationBean;
import com.example.zhunongzonghe.beans.ZnShoppingcartBean;
import com.example.zhunongzonghe.dao.ZnOrderDetailsMapper;
import com.example.zhunongzonghe.dao.ZnOrderMapper;
import com.example.zhunongzonghe.dao.ZnShoppingcartMapper;
import com.example.zhunongzonghe.entity.*;
import com.example.zhunongzonghe.model.ShopModel;
import com.example.zhunongzonghe.service.IZnShoppingcartService;
import com.example.zhunongzonghe.service.ZnFarmSpecificationService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
public class ZnShoppingcartServiceImpl extends ServiceImpl<ZnShoppingcartMapper, ZnShoppingcart> implements IZnShoppingcartService {
    @Autowired
    ZnShoppingcartMapper znShoppingcartMapper;
    @Autowired
    ZnOrderMapper znOrderMapper;
    @Autowired
    ZnOrderDetailsMapper znOrderDetailsMapper;
    @Resource
    private ZnFarmSpecificationService specificationService;
    /**
     * 查询当前用户购物车
     * @param username
     * @return
     */
    @Override
    public List<ZnShoppingcartBean> showCart(String username) {
        MPJLambdaWrapper<ZnShoppingcart> wrapper = new MPJLambdaWrapper<ZnShoppingcart>()
                .selectAll(ZnShoppingcart.class)
                .select(ZnFarmproduce::getFarmName)
                .select(ZnFarmproduce::getFarmIntro)
                .select(ZnFarmproduce::getShopPicture)
                .eq(ZnShoppingcart::getUsername,username)
                .leftJoin(ZnFarmproduce.class,ZnFarmproduce::getFarmId,ZnShoppingcart::getFarmId);
        List<ZnShoppingcartBean> beanList = znShoppingcartMapper.selectJoinList(ZnShoppingcartBean.class,wrapper);
        for (ZnShoppingcartBean cartBean: beanList
             ) {
            ZnFarmSpecification znFarmSpecification = specificationService.getById(cartBean.getSpecId());
            ZnFarmSpecificationBean specificationBean = BeanCopyUtils.copyBean(znFarmSpecification, ZnFarmSpecificationBean.class);
            cartBean.setSpecificationBean(specificationBean);
        }
        return beanList;
    }

    /**
     * 提交订单
     * @param znShoppingcartBean
     * @return
     */
    @Override
    public void commitOrder(ZnShoppingcartBean znShoppingcartBean, Principal principal) {
        String Number = generateOrderNumber();
        ZnOrder znOrder = new ZnOrder();
        znOrder.setOrderNumber(Number);
        znOrder.setUsername(principal.getName());
        znOrder.setAddressId(znShoppingcartBean.getAddressId());
        znOrder.setTotalPrice(znShoppingcartBean.getTotalPrice());
        znOrder.setCreateTime(LocalDateTime.now());
        znOrder.setPayStatus(0);
        znOrder.setDeliverStatus(0);
        znOrder.setTakeStatus(0);
        znOrder.setEvaluateStatus(0);
        znOrder.setLogisticsId(-1);
        znOrder.setCourierNumber("-1");
        znOrderMapper.insert(znOrder);
        List<ShopModel> shoppingcartList = znShoppingcartBean.getShoppingcartList();
        shoppingcartList.forEach(shopModel ->{
            ZnOrderDetails znOrderDetails = new ZnOrderDetails();
            znOrderDetails.setOrderId(znOrder.getOrderId());
            znOrderDetails.setCount(shopModel.getCount());
            znOrderDetails.setFarmId(shopModel.getFarmId());
            znOrderDetails.setAddressId(znShoppingcartBean.getAddressId());
            znOrderDetails.setTotalPrice(znShoppingcartBean.getTotalPrice());
            znOrderDetails.setUnitPrice(shopModel.getSpecificationBean().getZnSpecPrice());
            znOrderDetails.setSpecId(shopModel.getSpecificationBean().getId());
            znOrderDetails.setEvaStatus(0);
            znOrderDetailsMapper.insert(znOrderDetails);
        });
        znShoppingcartMapper.deleteBatchIds(znShoppingcartBean.getShoppingcartList());//清空购物车
    }

    public static String generateOrderNumber() {
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();

        // 格式化日期部分，例如：yyyyMMddHHmmss
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String datePart = dateFormat.format(new Date(timestamp));

        // 生成随机数部分，例如：0000 到 9999
        Random random = new Random();
        int randomPart = random.nextInt(10000);

        // 组合订单号
        String orderNumber = datePart + String.format("%04d", randomPart);

        return orderNumber;
    }
}
